package com.book.publishing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.common.entity.QueryRequest;
import com.book.publishing.entity.Bookman;
import com.book.publishing.mapper.BookmanMapper;
import com.book.publishing.service.BookmanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: 孙鑫
 * @Description:  20173952
 */

@Service
public class BookmanServiceImpl extends ServiceImpl<BookmanMapper, Bookman> implements BookmanService {
    @Autowired
    BookmanMapper bookmanMapper;

    @Override
    public List<Bookman> findBookman(Bookman bookman) {
        QueryWrapper<Bookman> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookman.getPubname())) {
            queryWrapper.eq("pubname", bookman.getPubname());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public Bookman findByName(String name) {
        QueryWrapper<Bookman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pubname", name);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Bookman> getSaleSort() {
        return bookmanMapper.getSaleSort();
    }

    @Override
    public List<Bookman> getBackSort() {
        return bookmanMapper.getBackSort();
    }

    @Override
    public IPage<Bookman> pageBookman(QueryRequest request, Bookman bookman) {
        QueryWrapper<Bookman> queryWrapper = new QueryWrapper<>();
        if (bookman.getSetuptime() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            queryWrapper.like("setuptime", simpleDateFormat.format(bookman.getSetuptime()));
        }
        if (StringUtils.isNotEmpty(bookman.getPubname())) {
            queryWrapper.like("pubname", bookman.getPubname());
        }
        if (StringUtils.isNotEmpty(bookman.getPubwhere())) {
            queryWrapper.like("pubwhere", bookman.getPubwhere());
        }
        Page<Bookman> bookmanIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookmanIPage, queryWrapper);
    }

    @Override
    public void addBookman(Bookman bookman) {
        this.save(bookman);
    }

    @Override
    public void updateBookman(Bookman bookman) {
        this.saveOrUpdate(bookman);
    }

    @Override
    public void deleteBookman(String[] bookman) {
        List<String> list = Arrays.asList(bookman);
        this.removeByIds(list);
    }

    @Override
    public List<Bookman> getBookmanByIds(String[] bookman) {
        List<String> list = Arrays.asList(bookman);
        return (List<Bookman>) listByIds(list);
    }
}
